package com.atguigu.customerdemo.controller;

import com.atguigu.customerdemo.bean.Customer;
import com.atguigu.customerdemo.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@Controller
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    //没有携带参数的请求
    @RequestMapping(value = "customer")
    public String getCustomer() {
        System.out.println("customer请求");
        return "success";
    }

    //携带参数的请求
    @RequestMapping(value = "customerByName")
    public String getCustomerByName(@RequestParam("name") String name) {
//        System.out.println("customerByName请求"+name);
        String result = customerService.getCustomerByName(name);

        return result;
    }

    //携带路径参数的请求
    @RequestMapping(value = "customerById/{id}")
    public String getCustomerById(@PathVariable("id") String value) {
//        System.out.println("customerById请求"+value);

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", value);

        Customer customer = customerService.getOne(queryWrapper);

        return value+"success:"+customer.toString();
    }

    @PostMapping("saveCustomer")
//    @RequestMapping(value = "saveCustomer",method = RequestMethod.POST)
    public String saveCustomer(@RequestBody Customer customer){
//        System.out.println("保存"+customer);
//        customerService.save(customer);
        //如果主键相同的话则更新，否则保存
//        customerService.saveOrUpdate(customer);
        customerService.saveCustomer(customer);
        return "保存成功";
    }



//    //比如：
//    @PostMapping("user-group")
//    public String saveUserGroup(@RequestBody UserGroup usergroup){
//
//    }


    @PostMapping("user-group-refresh/{id}")
    public void updataById(@PathVariable("id") String id,@RequestParam("busiDate") String busiDate){
        System.out.println(id+":"+busiDate);
    }
}
